import { loginInfoCheck, getLoginPicCode } from '../services/login.js';
import { getInfo, setInfo } from '@/utils/storage.js';

// eslint-disable-next-line import/no-anonymous-default-export
export default {
  namespace: 'user',
  state: {
    token: getInfo(),
    picUrl: '',
    picKey: ''
  },
  reducers: {
    setUserToken(state, { payload }) {
      // loaclStorage.setItem
      setInfo(payload.token)
      // console.log(payload);
      return {
        ...state,
        token: payload.token,
      }
    },

    setPicCode(state, { payload }) {
      const { base64, key } = payload.data.data;
      return {
        ...state,
        picUrl: base64,
        picKey: key
      }
    },
  },
  effects: {
    *loginCheck({ payload }, { call, put }) {
      const results = yield call(loginInfoCheck, payload);
      // console.log(results);
      yield put({
        type: 'setUserToken',
        payload: results,
      })
    },

    *getPicCode({ payload }, { call, put }) {
      const detail = yield call(getLoginPicCode);
      // console.log(detail);
      yield put({
        type: 'setPicCode',
        payload: detail
      });
    }
  },
}